/*
Copyright 2008, 2009 Joonas Lehtolahti, Jordan Toor

This file is part of Openfire-C.

Openfire-C is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Openfire-C is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Openfire-C.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "XfirePackets.h"

XFIREPHO XfirePHc_InstantMessage(non0string *sid, unsigned int imindex, non0string *message) {

	struct XfirePacket *p, *sp;
	unsigned int msgtype = 0;
	
	if (!sid) return NULL;
	if (!message) return NULL;
	
	p = XfirePHo_Init(0x0002, 2, 0);

	XfirePHo_AddAttribute(p, (unsigned const char *)"sid", XFP_TYPE_SID, sid);

		sp = XfirePHo_Init(0, 3, 0);

		XfirePHo_AddAttribute(sp, (unsigned const char *)"msgtype", XFP_TYPE_INT, &msgtype);
		XfirePHo_AddAttribute(sp, (unsigned const char *)"imindex", XFP_TYPE_INT, &imindex);
		XfirePHo_AddAttribute(sp, (unsigned const char *)"im", XFP_TYPE_STRING, message);

	XfirePHo_AddAttribute(p, (unsigned const char *)"peermsg", XFP_TYPE_PACKET, sp);

	return p;

}

XFIREPHO XfirePHc_IMack(non0string *sid, unsigned int imindex) {

	struct XfirePacket *p;
	struct XfirePacket *sp;
	unsigned int msgtype = 1;
	
	if (!sid) return NULL;
	
	p = XfirePHo_Init(0x0002, 2, 0);

	XfirePHo_AddAttribute(p, (unsigned const char *)"sid", XFP_TYPE_SID, sid);

		sp = XfirePHo_Init(0, 2, 0);

		XfirePHo_AddAttribute(sp, (unsigned const char *)"msgtype", XFP_TYPE_INT, &msgtype);
		XfirePHo_AddAttribute(sp, (unsigned const char *)"imindex", XFP_TYPE_INT, &imindex);

	XfirePHo_AddAttribute(p, (unsigned const char *)"peermsg", XFP_TYPE_PACKET, sp);

	return p;

}

XFIREPHO XfirePHc_TypingNotification(non0string *sid, unsigned int imindex, unsigned int typing) {

	struct XfirePacket *p;
	struct XfirePacket *sp;
	unsigned int msgtype = 3;
	
	if (!sid) return NULL;
	
	p = XfirePHo_Init(0x0002, 2, 0);

	XfirePHo_AddAttribute(p, (unsigned const char *)"sid", XFP_TYPE_SID, sid);
	
		sp = XfirePHo_Init(0, 3, 0);

		XfirePHo_AddAttribute(sp, (unsigned const char *)"msgtype", XFP_TYPE_INT, &msgtype);
		XfirePHo_AddAttribute(sp, (unsigned const char *)"imindex", XFP_TYPE_INT, &imindex);
		XfirePHo_AddAttribute(sp, (unsigned const char *)"typing", XFP_TYPE_INT, &typing);

	XfirePHo_AddAttribute(p, (unsigned const char *)"peermsg", XFP_TYPE_PACKET, sp);

	return p;

}

/*
 * XfirePHc_P2PExchange
 *   Creates network packet for exchanging P2P connectivity info
 * 
 * Parameters:
 *   sid       The SID number of the recipient
 *   ip        External IP address
 *   port      External port
 *   localip   Internal IP address
 *   localport Internal port
 *   nattype   NAT Type number
 *   salt      SHA1 checksum
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_P2PExchange(non0string *sid, unsigned int ip,
		unsigned int port, unsigned int localip, unsigned int localport,
		unsigned int nattype, non0string *salt) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int msgtype = 0x02;
	
	/* Required parameters */
	if (!sid) return NULL;
	if (!salt) return NULL;
	
	/* Packet '02 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0002, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"sid", XFP_TYPE_SID, sid);
		
		sp = XfirePHo_Init(0, 7, 0);
		XfirePHo_AddAttribute(sp, (unsigned const char *)"msgtype", XFP_TYPE_INT, &msgtype);
		XfirePHo_AddAttribute(sp, (unsigned const char *)"ip", XFP_TYPE_INT, &ip);
		XfirePHo_AddAttribute(sp, (unsigned const char *)"port", XFP_TYPE_INT, &port);
		XfirePHo_AddAttribute(sp, (unsigned const char *)"localip", XFP_TYPE_INT, &localip);
		XfirePHo_AddAttribute(sp, (unsigned const char *)"localport", XFP_TYPE_INT, &localport);
		XfirePHo_AddAttribute(sp, (unsigned const char *)"status", XFP_TYPE_INT, &nattype);
		XfirePHo_AddAttribute(sp, (unsigned const char *)"salt", XFP_TYPE_STRING, salt);
		
	XfirePHo_AddAttribute(p, (unsigned const char *)"peermsg", XFP_TYPE_PACKET, sp);
	
	/* Return completed packet */
	return p;
}
